<template>
  <div class="app-container">

    <SearchContent :queryParams="queryParams" @query="handleSearch" @reset="handleReset">
      <el-col :span="6">
        <el-form-item label="工单状态:" prop="workOrderStatus">
          <el-select v-model="queryParams.workOrderStatus" style="width: 100%" placeholder="请选择工单状态">
            <el-option v-for="item in dict.type.work_order_status" :label="item.label"
                       :value="item.value" :key="item.value" />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="车牌号/车架号:" prop="carNoVinNo">
          <el-input v-model="queryParams.carNoVinNo" clearable placeholder="请输入车牌号/车架号"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="车辆产权:">
          <el-select filterable v-model="queryParams.propertyRightId" clearable placeholder="请选择车辆产权"
                     style="width: 100%">

            <el-option v-for="item in companyOriginList" :label="item.deptName" :value="item.deptId"
                       :key="item.deptId" />

          </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="批次号:" prop="flowBusinessId">
          <el-input v-model="queryParams.flowBusinessId" clearable placeholder="请输入批次号"></el-input>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="车辆归属:">
          <el-select filterable v-model="queryParams.ascriptionId" clearable placeholder="请选择车辆归属"
                     style="width: 100%">

            <el-option v-for="item in companyOriginList" :label="item.deptName" :value="item.deptId"
                       :key="item.deptId" />

          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="品牌/车系/车型:">
          <linkage-select @changeNode="linkageChange" v-model="linkageList" placeholder="请选择品牌/车系/车型"/>
        </el-form-item>
      </el-col>

      <!--      <el-col :span="6">-->
      <!--        <el-form-item label="采购禀议编号:" prop="procurementReportNumber">-->
      <!--          <el-input v-model="queryParams.procurementReportNumber" clearable placeholder="请输入采购禀议编号"></el-input>-->
      <!--        </el-form-item>-->
      <!--      </el-col>-->
      <el-col :span="6">
        <el-form-item label="客户名称:" prop="customerName">
          <el-input v-model="queryParams.customerName" clearable placeholder="请输入客户名称"></el-input>
        </el-form-item>
      </el-col>
      <!--      <el-col :span="6">-->
      <!--        <el-form-item label="保险到期日:" prop="customerName">-->
      <!--          <el-date-picker v-model="timeData" type="daterange"-->
      <!--                          style="width: 100%" value-format="yyyy-MM-dd"-->
      <!--                          range-separator="至" @change="handleTime"-->
      <!--                          start-placeholder="开始日期" end-placeholder="结束日期">-->
      <!--          </el-date-picker>-->
      <!--        </el-form-item>-->
      <!--      </el-col>-->
    </SearchContent>
    <div class="card-wrap card-padding">
      <div class="table-title-wrap justify-end">
        <e-button type="primary" size="mini" @click="handleBatch" :disabled="idsList.length === 0"
                  v-hasPermi="['renewal:renewalList:batch']">批量处理
        </e-button>
      </div>

      <table-page :loading="loading" :dataList="dataList" :total="total" :page.sync="queryParams.pageNum"
                  @setSizeCurrent="handleList"
                  @selectionChange="handleSelectionChange" :isSelect="true" :limit.sync="queryParams.pageSize" border>

        <template slot="tableColAc">
          <el-table-column label="操作" align="center" fixed="left" width="100">
            <template slot-scope="{row}">
              <el-button size="mini" type="text" @click="handleView(row, 'view')"
                         v-if="row.workOrderStatus !== 1"
                         v-hasPermi="['renewal:renewalList:view']">查看
              </el-button>
              <el-button size="mini" type="text" v-hasPermi="['renewal:renewalList:handle']"
              v-if="row.workOrderStatus == 1 || row.workOrderStatus == 5"
                         @click="handleWork(row)">处理
              </el-button>
            </template>
          </el-table-column>

        </template>

        <template slot="tableCol">
          <el-table-column align="center" label="保养工单编号" prop="workOrderCode" min-width="180"/>

          <el-table-column align="center" label="批次号" prop="flowBusinessId" show-overflow-tooltip
                           min-width="180"/>

          <el-table-column align="center" label="车牌号" min-width="100" show-overflow-tooltip prop="carNo"/>

          <el-table-column align="center" label="车架号" prop="vinNo" show-overflow-tooltip min-width="150"/>

          <el-table-column align="center" label="品牌/车系/车型" show-overflow-tooltip width="320">
            <template #default="{row}">
              {{ row.brandName }}{{ row.seriesName ? '/' + row.seriesName : '' }}{{
                row.modelName ? '/' + row.modelName
                    : ''
              }}
            </template>
          </el-table-column>

          <el-table-column align="center" label="车辆产权" show-overflow-tooltip min-width="160" prop="propertyRightName"/>
          <el-table-column align="center" label="车辆归属" show-overflow-tooltip min-width="160" prop="ascription"/>
          <el-table-column align="center" label="车辆运营" show-overflow-tooltip min-width="120" prop="operationsName"/>
          <el-table-column align="center" label="车辆大区" show-overflow-tooltip min-width="160">
            <template #default="{row}">
              <span v-if="row.region">{{row.region}} </span>

            </template>
          </el-table-column>

          <el-table-column align="center" label="工单类型" min-width="100">
            <template #default="{row}">
              {{ dictsName(dict.type.work_order_type, row.workOrderType) }}工单
            </template>
          </el-table-column>
          <el-table-column align="center" label="审批状态" show-overflow-tooltip min-width="140" prop="expenseStatus" />
          <el-table-column align="center" label="客户名称" min-width="160" show-overflow-tooltip prop="customerName"/>

          <el-table-column align="center" label="工单状态" min-width="100">
            <template #default="{row}">
              {{ dictsName(dict.type.work_order_status, row.workOrderStatus) }}
            </template>
          </el-table-column>

          <el-table-column align="center" label="车辆状态" min-width="160">
            <template #default="{row}">
              {{ dictsName(dict.type.car_business_type, row.businessType) }}
            </template>
          </el-table-column>

          <el-table-column align="center" label="订单编号" min-width="200" show-overflow-tooltip prop="orderCode"/>

          <el-table-column align="center" label="产品类型" min-width="120" show-overflow-tooltip prop="contractType"/>

          <el-table-column align="center" label="交强险公司" min-width="160" show-overflow-tooltip prop="jqxCompany"/>

          <el-table-column align="center" label="交强险到期日" show-overflow-tooltip min-width="140"
                           prop="jqxExpectEndTime">
            <template #default="{row}">
              {{ row.jqxActualEndTime ? $dayjs(row.jqxActualEndTime).format('YYYY-MM-DD') : '' }}
            </template>
          </el-table-column>

          <el-table-column align="center" label="商业险公司" min-width="160" show-overflow-tooltip prop="syxCompany"/>

          <el-table-column align="center" label="商业险到期日" min-width="120" show-overflow-tooltip
                           prop="syxExpectEndTime">
            <template #default="{row}">
              {{ row.syxActualEndTime ? $dayjs(row.syxActualEndTime).format('YYYY-MM-DD') : '' }}
            </template>
          </el-table-column>

          <el-table-column align="center" label="处理人" min-width="120" prop="processedByName" show-overflow-tooltip/>

          <el-table-column align="center" label="处理时间" min-width="120">
            <template #default="{row}">
              {{ row.processedByTime ? $dayjs(row.processedByTime).format('YYYY-MM-DD') : '' }}
            </template>

          </el-table-column>
          <el-table-column align="center" label="工单创建时间" min-width="120" prop="createTime">
            <template #default="{row}">
              {{ row.createTime ? $dayjs(row.createTime).format('YYYY-MM-DD') : '' }}
            </template>

          </el-table-column>
        </template>
      </table-page>
    </div>
  </div>
</template>

<script>
import CitySelect from "@/views/components/citySelect.vue";
import LinkageSelect from "../../components/linkageSelect";
import {getRenewalList} from "@/api/cwgd/renewal.js";
import SearchContent from "@/components/SearchContent/index.vue";
import { listDept } from "@/api/system/dept";
export default {
  name: "renewalList",
  dicts: ["car_business_type", "work_order_type","work_order_status"],
  components: {CitySelect, LinkageSelect, SearchContent},
  data() {
    return {
      loading: false,
      showSearch: false,
      linkageList: [],
      citySelection: [],
      timeData: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carNoVinNo: "",
        procurementReportNumber: "",
        brandName: "",
        seriesName: "",
        modelName: "",
        contractReportNumber: "",
        belongCityName: "",
        workOrderStatus: "1",
        customerName: "",
        workOrderType: "2",
        startTime: "",
        endTime: "",
        propertyRightId: "",
        ascriptionId: "",
        flowBusinessId:""
      },
      dataList: [],
      total: 0,
      idsList: [],
      companyOriginList: [],
    };
  },
  computed: {
    dictsName() {
      return (arr, type) => {
        let name = "";
        arr.map((v) => {
          if (type == v.value) {
            name = v.label;
          }
        });
        return name;
      };
    },
  },
  watch: {},
  created() {
    this.handleList();
    this.getCompanyOrigin();
  },
  mounted() {
  },
  methods: {
    getCompanyOrigin() {
      listDept({
        deptType: "2", //组织类型（1部门 2公司）
        regionId: null, //所在城市（省市区id）
      }).then((res) => {
        this.companyOriginList = res.data;
      });
    },

    //车辆归属
    citySelectionChange(val) {
      if (val.length > 0) {
        const nodes = Array.from(val, (item) => ({
          value: item.value,
          label: item.label,
          pv: item.parent.value,
          pl: item.parent.label,
        }));
        this.queryParams.belongCityName = nodes[0].label;
        this.citySelection = [nodes[0].pv, nodes[0].value];
      } else {
        this.citySelection = val;

        this.queryParams.belongCityName = "";
      }
    },

    //品牌/车系/车型
    linkageChange(val) {
      this.linkageList = val;
      if (val) {
        this.queryParams.brandName = val[0] ? val[0].name : "";
        this.queryParams.seriesName = val[1] ? val[1].name : "";
        this.queryParams.modelName = val[2] ? val[2].name : "";
      } else {
        this.queryParams.brandName = "";
        this.queryParams.seriesName = "";
        this.queryParams.modelName = "";
      }
      this.$forceUpdate();
    },

    //时间
    handleTime(val) {
      this.timeData = val;
      this.queryParams.startTime = val[0];
      this.queryParams.endTime = val[1];
    },

    //搜索
    handleSearch() {
      this.queryParams.pageNum = 1;
      this.handleList();
    },

    //重置
    handleReset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        carNoVinNo: "",
        procurementReportNumber: "",
        brandName: "",
        seriesName: "",
        modelName: "",
        contractReportNumber: "",
        belongCityName: "",
        workOrderStatus: "1",
        customerName: "",
        workOrderType: "2",
        startTime: "",
        endTime: "",
        propertyRightId: "",
        ascriptionId: "",
        flowBusinessId:""
      };
      this.citySelection = [];
      this.linkageList = [];
      this.timeData = [];
      this.handleList();
    },

    //查看
    handleView(row) {
      this._toPage({
        path: "/cwgd/newCarInsuranceDetails",
        name:'AddRenewal',
        query: {
          id: row.id,
          carId: row.carId,
          channels: row.insuranceChannels,
          type: "view",
          flowId: row?.flowId || null
        },
      });
    },

    //处理
    handleWork(row) {
      let arr = []
      arr.push(row.id)
      this._toPage({
        path: "/cwgd/addRenewal",
        name:'AddRenewal',
        query: {
          id: arr,
          type: 'edit',
          flowId: row?.flowId || null,
        },
      });
    },

    //批量处理
    handleBatch() {
      if (this.idsList.length > 0) {
        this._toPage({
          path: "/cwgd/addRenewal",
          name:'AddRenewal',
          query: {
            id: this.idsList,
            type: 'edit'
          },
        });
      } else {
        this.$message({
          message: "请勾选续保工单",
          type: "warning",
        });
      }
    },

    getList() {
    },

    //多选
    handleSelectionChange(val) {
      this.idsList = val.map((a) => {
        return a.id;
      });
    },

    handleList() {
      this.loading = true;
      getRenewalList(this.queryParams).then((res) => {
        if (res.code === 200) {
          this.total = res.total;
          this.dataList = res.rows;
          this.loading = false;
        }
      }).finally(() => {
        this.loading = false;
      })
    },
  },
};
</script>

<style lang="scss" scoped>
.el-form {
  padding: 18px 20px;
  padding-bottom: 8px;
  background-color: #fff;
  border-radius: 8px;
  margin-bottom: 16px;

  ::v-deep .el-form-item {
    margin-bottom: 10px;
  }

  ::v-deep .el-form-item__label {
    font-size: 14px;
    font-family: 400;
  }
}
</style>



